package com.example.linearlayoutdemo;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

import androidx.activity.OnBackPressedCallback;
import androidx.appcompat.app.AppCompatActivity;

public class InterestActivity extends AppCompatActivity {

    private CheckBox cbSports, cbReading, cbMusic;
    private Button btnBack, btnConfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("生命周期","onCreate:页面被创建了");
        setContentView(R.layout.activity_interest);

        // 初始化控件
        initViews();

        // 设置按钮点击事件
        setButtonListeners();

        this.getOnBackPressedDispatcher().addCallback(this,
                new OnBackPressedCallback(true) { // true表示"启用回调"
                    @Override
                    public void handleOnBackPressed() {
                        handleBack();
                    }
                });
    }

    private void initViews() {
        // 初始化复选框
        cbSports = findViewById(R.id.cb_sports);
        cbReading = findViewById(R.id.cb_reading);
        cbMusic = findViewById(R.id.cb_music);

        // 初始化按钮
        btnBack = findViewById(R.id.btn_back);
        btnConfirm = findViewById(R.id.btn_confirm);
    }

    private void setButtonListeners() {
        // 返回按钮点击事件
        btnBack.setOnClickListener(v -> finish());

        // 确定按钮点击事件
        btnConfirm.setOnClickListener(v -> {
            handleBack();
        });
    }



    private void handleBack() {
        // 收集选中的兴趣
        StringBuilder selectedInterests = new StringBuilder();

        if (cbSports.isChecked()) {
            selectedInterests.append("运动,");
        }
        if (cbReading.isChecked()) {
            selectedInterests.append("阅读,");
        }
        if (cbMusic.isChecked()) {
            selectedInterests.append("音乐,");
        }

        // 移除最后一个逗号
        if (selectedInterests.length() > 0) {
            selectedInterests.deleteCharAt(selectedInterests.length() - 1);
        } else {
            selectedInterests.append("未选择兴趣");
        }

        // 将结果返回给上一个页面
        Intent intent = new Intent();
        intent.putExtra("selectedInterests", selectedInterests.toString());
        setResult(RESULT_OK, intent);

        // 关闭当前页面，返回上一页
        finish();
    }


    @Override
    protected void onStart() {
        super.onStart();
        Log.d("生命周期","onStart：可见但不可操作");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("生命周期","onResume：页面可见可操作");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("生命周期","onPause：页面部分可见或完全被遮挡");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("生命周期","onStop：页面完全看不见了");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("生命周期","onRestart：页面即将重新可见");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("生命周期","onDestroy：页面即将销毁");
    }
}
